package pl.edu.mimuw.irs.rcp.controller.editor;

import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import pl.edu.mimuw.irs.core.model.design.IRSRequirement;
import pl.edu.mimuw.irs.core.model.design.processing.Role;
import pl.edu.mimuw.irs.rcp.controller.editor.input.RequirementsEditorInput;
import pl.edu.mimuw.irs.rcp.controller.editor.util.RequirementsTreeWrapper;
import pl.edu.mimuw.irs.rcp.util.MessageCode;
import pl.edu.mimuw.irs.rcp.util.UITexts;

public class RequirementsEditor extends EditorPart {
	
	public static final String ID = "pl.edu.mimuw.irs.editor.requirements";
	public static final String COMMAND_ID = "pl.edu.mimuw.irs.command.editrequirements";

	// *** Attributes
	
	private IRSRequirement requirement;
	private Map<String, Role> rolesMap;
	private Label lblName;
	private Text txtName;
	private Label lblRoles;
	private RequirementsTreeWrapper treRoles;
	public IRSRequirement getRequirement() {
		return requirement;
	}
	public void setRequirement(IRSRequirement requirements) {
		this.requirement = requirements;
	}
	public Map<String, Role> getRolesMap() {
		if (rolesMap == null) rolesMap = new LinkedHashMap<String, Role>();
		return rolesMap;
	}
	public void setRolesMap(Map<String, Role> rolesMap) {
		this.rolesMap = rolesMap;
	}
	
	// *** Methods

	@Override
	public void doSave(IProgressMonitor monitor) {
		// XXX : container editor save

	}

	@Override
	public void doSaveAs() {
		// XXX : Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
		setRequirement(((RequirementsEditorInput) input).getRequirement());
		setRolesMap(((RequirementsEditorInput) input).getRolesMap());
		setPartName(getRequirement().getId());
	}

	@Override
	public boolean isDirty() {
		if (getRequirement().getId().equals(txtName.getText())) {
			return false;
		}
		return true;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// XXX save allowed
		return true;
	}

	@Override
	public void createPartControl(Composite parent) {
		GridData gridData;
		Composite composite =  new Composite(parent, SWT.NULL);

	    // create the desired layout for this wizard page
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.BEGINNING;	
		
		lblName = new Label(composite, SWT.NONE);
		lblName.setText(UITexts.get(MessageCode.REQUIREMENTS_EDITOR_NAME));				
		txtName = new Text(composite, SWT.BORDER);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		//gridData.horizontalSpan = ncol - 1;
		txtName.setLayoutData(gridData);
		txtName.setText(getRequirement().getId());
		
		gridData = new GridData();
		lblRoles = new Label(composite, SWT.NONE);
		lblRoles.setText(UITexts.get(MessageCode.REQUIREMENTS_EDITOR_CONTENTS));
		gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
		treRoles = new RequirementsTreeWrapper(composite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL, 
				getRequirement().getRequirements());
		treRoles.setLayoutData(gridData);
		//tblRoles.redraw();
		//setErrorMessage(null);
		//setMessage(UITexts.get(MessageCode.PROJECT_WIZARD_PROJECT_INTRO));
	
	}

	@Override
	public void setFocus() {
	}
}
